package com.weichuang.service;

import com.weichuang.dao.UserMapper;
import com.weichuang.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ServiceDemo {
    public static void main(String[] args) throws IOException {
        //获取核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获取session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取Mybatis框架生成的UserMapper接口的实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //查询所有用户
        /*List<User> all = userMapper.findAll();
        System.out.println(all);

        //根据id查询用户信息
        User user = userMapper.findById(3);
        System.out.println(user);*/

        //添加
        /*User user1 = new User();
        user1.setUsername("索隆");
        user1.setPassword("1234");
        userMapper.save(user1);
        sqlSession.commit();*/

        //删除
        /*userMapper.delete(5);
        sqlSession.commit();*/
        //修改
        User user2 = new User();
        user2.setId(3);
        user2.setUsername("山治");
        user2.setPassword("1234");
        userMapper.update(user2);
        sqlSession.commit();
    }
}
